/**
 * 自定义函数
 * */

var userLogin = 'userInfo'; //登录信息
var pageSize = 10;
var apiMethod = 'post';
var apiDomain = '/api/h5/';
var bomMenuArys = [
    {
        id: 1, title: '工作台', link: '/h5/index.html',
        icon0: '/h5/imgs/con20.png', icon1: '/h5/imgs/con21.png',
    },
    {
        id: 2, title: '统计', link: '/h5/ws-tji/person.html',
        icon0: '/h5/imgs/con30.png', icon1: '/h5/imgs/con31.png',
    }
];


(function () {
    var path = window.location.pathname;
    var uinfo = getUserInfo();
    if (path != '/h5/login.html') {
        if (!uinfo) {
            window.location.href = '/h5/login.html';
        }
        if (path == '/h5/index.html' || path == '/h5/workspace' || path == '/h5/tongji.html') {
            bomMenu();
        }
        guoqi();
    }
    config();
})();


//是否过期
function config(){
    $.ajax({
        type: 'get',
        url: '/api/admin/config/index',
        data: {},
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                $('body').html(res.msg);
                return
            }
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//判断过期
function guoqi(){
    var uinfo = getUserInfo();
    var formData = {
        logtime: uinfo.logtime,
    };
    $.ajax({
        type: 'post',
        url: apiDomain + 'guoqi',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                errMsg({msg: res.msg}); return
            }
            if (res.code == 1) {
                msgPop({msg: res.msg});
                setTimeout(function(){
                    window.location.href = '/h5/login.html';
                }, 500);
            }
        },
        error: function (err) {
            console.log(err);
        }
    });
}


//退出
function logout(){
    delCache({key: userLogin});
    window.location.href = '/h5/login.html';
}


//头部
function getHeader(){
    var h = '';
    h += '<div class="head" onclick="myinfoPop();">';
    h += '<img src="/h5/imgs/user-head.png"/>';
    h += '</div>';
    h += '<div class="title">' + headTitle + '</div>';
    h += '';
    $('#header').html(h);
}


//我的资料弹窗
function myinfoPop(){
    var uinfo = getUserInfo();
    console.log(uinfo);
    var h = '';
    h += '<style type="text/css">#myPop .popMask { width: 100vw; height: 100vh; background: rgba(0,0,0,0.5); position: fixed; left: 0; top: 0; z-index: 100; } #myPop .popCnt { padding: 20px; width: 80vw; height: 100vh; background: #fff; position: fixed; left: 0; top: 0; z-index: 100; } #myPop .uhead { width: 50px; height: 50px; overflow: hidden; } #myPop img { width: 50px; } #myPop .group { margin: 20px 0; font-weight: bolder; color: #555; } #myPop .popClose { color: red; position: fixed; left: 80vw; top: 1vh; z-index: 100; } #myPop a { text-decoration: none; color: red; }</style>';
    h += '<div class="popMask" onclick="$(\'#myPop\').html(\'\');"></div>';
    h += '<div class="popCnt">';
    h += '<div class="uhead"><img src="/h5/imgs/user-head.png"/></div>';
    h += '<div class="group">';
    h += '账号：' + uinfo.uname;
    h += '</div>';
    h += '<div class="group">';
    h += '角色：' + uinfo.role_name;
    h += '</div>';
    h += '<div class="group">';
    h += '部门：' + uinfo.depart_name;
    h += '</div>';
    h += '<div class="group">';
    h += '<a href="javascript:;" style="color: #0088CC;" onclick="pwdPop();">修改密码</a>';
    h += '</div>';
    h += '<div class="group">';
    h += '<a href="javascript:;" onclick="logout();">退出登录</a>';
    h += '</div>';
    h += '</div>';
    h += '<div class="popClose" onclick="$(\'#myPop\').html(\'\');" onclick="$(\'#myPop\').html(\'\');">隐藏</div>';
    $('#myPop').html(h);
}


// 修改密码界面
function pwdPop(){
    var path = window.location.pathname;
    window.location.href = '/h5/ws-sys/pwd.html?preUrl=' + path;
}


// 底部菜单
function bomMenu() {
    //bomMenuHtml();
    var formData = {
        uid: 0, role_id: 0,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'bommenu',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: '权限错误'});
                return
            }
            bomMenuHtml(res.data);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//底部菜单字符串
function bomMenuHtml(){
    var h = '', i = 0, ms = bomMenuArys;
    for (; i < ms.length; i ++) {
        if (bm_id == ms[i].id) {
            h += '<div class="li click" onclick="toMenu(' + ms[i].id + ');">';
            h += '<img src="' + ms[i].icon1 + '"/>';
        } else {
            h += '<div class="li" onclick="toMenu(' + ms[i].id + ');">';
            h += '<img src="' + ms[i].icon0 + '"/>';
        }
        h += ' <br>';
        h += ms[i].title;
        h += '</div>';
    }
    $('#bomMenu').html(h);
}
//切换菜单
function toMenu(v){
    console.log(v, bomMenuArys);
    var link = '', ms = bomMenuArys, i = 0;
    for (i; i < ms.length; i ++) {
        if (v == ms[i].id) {
            link = ms[i].link;
        }
    }
    window.location.href = link + '?jstime=' + new Date().getTime();
}


//显示字符串前几个字符
function strPrefix(str, n){
    if (str.length <= n) {
        return str;
    } else {
        return str.substring(0, n) + '...';
    }
}


//生成随机数
function randInt(min, max) {
    // 加1是因为我们想要包含max值在内
    return Math.floor(Math.random() * (max - min + 1)) + min;
}


////分页字符串
//function pageHtml(e){
//    console.log(e);
//    var start = e.start, limit = e.limit, total = e.total, url = e.url;
//    var pageTotal = Math.ceil(total / limit);
//    var h = '';
//    h += '<style type="text/css"> #pageHtml {margin: 10px 0; width: 100%; text-align: center; color: #555; } #pageBtn {  color:#555; } #pageBtn a { padding: 5px 10px; color: #0088cc; } </style>';
//    h += '显示 <span id="pageCurr">' + start + '</span> / <span id="pageTotal">' + pageTotal + '</span> 页，';
//    h += '<span id="pageBtn">';
//    h += '<a href="javascript:;" onclick="pageHome();">首页</a>';
//    h += '<a href="javascript:;" onclick="pagePre();">上一页</a>';
//    //h += '<a href="javascript:;" onclick="msgPop({msg: \'当前第' + start + '页\'});">' + start + '</a>';
//    h += '<a href="javascript:;" onclick="pageNext();">下一页</a>';
//    h += '<a href="javascript:;" onclick="pageEnd();">尾页</a>';
//    h += '</span>';
//    $('#pageHtml').html(h);
//}
////首页
//function pageHome(){
//    window.location.href = pages.url;
//}
////上一页
//function pagePre(){
//    var start = pages.start * 1 - 1;
//    if (start <= 0) {
//        msgPop({msg: '已经是第一页了'});
//        return
//    }
//    window.location.href = pages.url + '?start=' + start;
//}
////下一页
//function pageNext(){
//    var start = pages.start * 1 + 1;
//    var pageTotal = Math.ceil(pages.total / pages.limit);
//    if (start >= pageTotal) {
//        msgPop({msg: '已经是最后一页了'});
//        return
//    }
//    window.location.href = pages.url + '?start=' + start;
//}
////尾页
//function pageEnd(){
//    var pageTotal = Math.ceil(pages.total / pages.limit);
//    window.location.href = pages.url + '?start=' + pageTotal;
//}


//抓图
function uploadImage() {
    var fileObj = $('#bigUpload')[0],
        reader = new FileReader();
    reader.readAsDataURL(fileObj.files[0]); //这一步是关键
    reader.onload = function (res){
        //console.log(res.target.result);
        showImg({img: res.target.result});
    }
}


//登录aid
function getAid(){
    var adminInfo = getAdminInfo();
    if (!adminInfo) {
        msgPop({msg: '没有登录'});
        setTimeout(function () {
            location.href = '/ad369/login.html';
        }, 1000);
        return
    }
    return adminInfo.aid;
}


//自定义弹窗
function msgPop(e){
    var cw = document.documentElement.clientWidth;
    var ch = document.documentElement.clientHeight;
    var msg = e.msg;
    var h = '';
    h += '<div style="position: fixed; top: 0; left: 0; z-index: 10000;">';
    h += '<div style="width: ' + (cw) + 'px; height: ' + (ch) + 'px; background: #000; opacity: 0.2; position: fixed; top: 0; left: 0; z-index: 10000;"></div>';
    h += '<div style="padding: 10px 20px; width: ' + (cw * 0.5) + 'px; text-align: center; background: #000; color: #FFF; border-radius: 5px; position: fixed; bottom: 40%; left: ' + ((cw * 0.5 - 40) / 2) + 'px; z-index: 10000;">'+msg+'</div>';
    h += '</div>';
    h += '<script>setTimeout(function(){$(\'#msgPop\').html(\'\');}, 2000)</script>';
    $('#msgPop').html(h);
}


//获取登录信息
function getUserInfo(){
    return getCache(userLogin);
}


//设置登录信息
function setUserInfo(e){
    return setCache({key: userLogin, val: e.val});
}


//获取地址栏中参数
function getParamByUrl(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r==null) return '';
    return  unescape(r[2]);
}


//设置缓存
function setCache(e){
    var key = e.key, val = JSON.stringify(e.val);
    window.localStorage.setItem(key, val);
}


//获取缓存
function getCache(key){
    var cache = window.localStorage.getItem(key);
    if (!cache) { return ''; }
    return JSON.parse(cache);
}


//删除指定缓存
function delCache(e){
    var key = e.key;
    window.localStorage.removeItem(key);
}


//清理缓存
function clearCache(){
    window.localStorage.clear();
}